<template>
  <div id="classify">  
    <Popover :message="message"  v-show="isShow"/>
    <ClassifyNavBar class="nav-bar" @titleClick="titleClick" ref="Nav" />
    <Scroll
      class="content"
      :probeType="3"
      :pullUpLoad="true"
      ref="scroll"
      @scroll="scroll"
    >
      <template>
        <Swiper :swiperImages="swiperImages" />
        <OneInfo :goods="goods" />
        <ShopInfo :shop="shop" />
        <Info :info="info" @imageLoad="imageLoad" />
        <ParamInfo :param="param" ref="param" />
        <Comment :comment="comment" ref="comment" />
        <GoodsList :goods="recommend" ref="recommend" />
      </template>
    </Scroll>
  
    <BackTop class="back-top" @click.native="backTop" v-show="isBackTop" />
    <ClassifyFooterBar @addShop="addShop" />
  </div>
</template>

<script>
import Swiper from '@/components/common/classify/classifySwiper'
import GoodsList from '@/components/common/goods/goodsList'
import ClassifyNavBar from '@/components/common/classify/classifyNavBar'
import OneInfo from '@/components/common/classify/1.commodityInfo'
import ShopInfo from '@/components/common/classify/2.shopInfo'
import Info from '@/components/common/classify/3.Info'
import ParamInfo from '@/components/common/classify/4.param'
import Comment from '@/components/common/classify/5.comment'
import ClassifyFooterBar from '@/components/common/classify/classifyFooterBar'

import BackTop from '@/components/content/backTop/'
import Scroll from '@/components/content/scroll/'
import Popover from '@/components/content/popover/'
//事件总线
import Bus from '@/utils/bus'
import {
  getProductData,
  Title,
  Shop,
  Param,
  getRecommend,
} from '@/utils/classify/'
export default {
  name: 'classify',
  components: {
    ClassifyNavBar,
    Swiper,
    OneInfo,
    ShopInfo,
    Info,
    Scroll,
    ParamInfo,
    Comment,
    GoodsList,
    BackTop,
    ClassifyFooterBar,
    Popover
  },
  data() {
    return {
      iid: null,
      swiperImages: [], //轮播图图片
      goods: {}, // 商品信息
      shop: {}, // 店铺信息
      info: {}, // 商品的详情图片和
      param: {}, //商品参数
      comment: {}, //评论
      recommend: [], // 推荐
      ItemImageLister: null,
      position: null,
      isBackTop: false,
      titleoffSetTop: [], //商品参数 评论 评论 距离头部的距离
      i: 0,
      message: '',
      isShow:false
    }
  },
  created() {
    // 报存商品的 id
    this.iid = this.$route.params.iid
    // 根据id 亲求对应的商品数据
    this.GetProductData(this.iid)
    this.GetRecommend()
  },
  updated(){
    this.$refs.scroll.refresh()
  },
  mounted() {
    this.ItemImageLister = () => {
      this.debounce(this.$refs.scroll.refresh, 500)
    }
    Bus.$on('goodItemImageLoad', this.ItemImageLister)
  },
  destroyed() {
    // 离开路由时销毁该事件
    Bus.$off('goodItemImageLoad', this.ItemImageLister)
  },
  methods: {
    // 防抖
    debounce(func, delay) {
      let time = null
      return function(...args) {
        if (time) {
          clearTimeout(time)
        }
        time = setTimeout(() => {
          func(this, args)
        }, delay)
      }
    },
    // title 点击事件
    titleClick(index) {
      this.$refs.scroll.Back(0, -this.titleoffSetTop[index] + 20, 300)
    },
    imageLoad() {
      this.$refs.scroll.refresh()
      // 所有异步图片加载完成后 获取商品参数 评论 评论组件 距离头部的值
      this.titleoffSetTop.push(0)
      this.titleoffSetTop.push(this.$refs.param.$el.offsetTop)
      this.titleoffSetTop.push(this.$refs.comment.$el.offsetTop)
      this.titleoffSetTop.push(this.$refs.recommend.$el.offsetTop)
      this.titleoffSetTop.push(Number.MAX_VALUE)
    },
    //回到顶部
    backTop() {
      this.$refs.scroll.Back(0, 0, 200)
    },
    // 获取 滚动的位置
    scroll(position) {
      this.position = -position.y
      // 回到顶部
      if (this.position > 1000) {
        this.isBackTop = true
      } else {
        this.isBackTop = false
      }
      // 滚动内容显示对应标题
      // const length = this.titleoffSetTop.length
      for (let i = 0; i < this.titleoffSetTop.length; ++i) {
        // if (
        //   this.i != i &&((i < length - 1 &&
        //     this.position >= this.titleoffSetTop[i] &&
        //     this.position <= this.titleoffSetTop[i + 1]) ||
        //   (i === length - 1 &&
        //     this.position >= this.titleoffSetTop[i]))
        // ) {
        //   this.i =i
        //    this.$refs.Nav.Myindex = this.i
        // }
        // 优化
        if (
          this.i != i &&
          this.position >= this.titleoffSetTop[i] &&
          this.position <= this.titleoffSetTop[i + 1]
        ) {
          this.i = i
          this.$refs.Nav.Myindex = this.i
        }
      }
    },
    // 添加到购物车
    addShop() {
      // 1. 获取购物车需要展示的信息
      const product = {}
      product.id = this.iid // 商品id
      product.img = this.swiperImages[0] // 图片
      product.title = this.goods.title // 标题
      product.desc = this.goods.desc // 商品描述
      product.Price = this.goods.nowPrice // 商品价格

      this.$store.dispatch('addproduct', product).then((res) => {
        this.message = res
      })
      this.isShow = true
      setTimeout(()=>{ 
        this.isShow = false
      },1000)
    },
    // 请求
    GetProductData(iid) {
      getProductData(iid).then((res) => {
        const data = res.data.result
        this.swiperImages = data.itemInfo.topImages
        this.goods = new Title(
          data.itemInfo,
          data.columns,
          data.shopInfo.services
        )
        this.shop = new Shop(data.shopInfo)
        this.info = data.detailInfo
        this.param = new Param(data.itemParams.info, data.itemParams.rule)
        if (data.rate.cRate.length !== 0) {
          this.comment = data.rate.list[0]
        }
      })
    },
    GetRecommend() {
      getRecommend().then((res) => {
        this.recommend = res.data.data.list
      })
    },
  },
}
</script>
<style scoped>
#classify {
  position: relative;
  z-index: 9;
  background-color: #fff;
  height: 100vh;
}
.nav-bar {
  position: relative;
  z-index: 9;
  background-color: #fff;
}
.content {
  height: calc(100% - 88px);
}
.back-top {
  position: absolute;
  z-index: 5;
  bottom: 60px;
  right: 10px;
}
</style>
